<template>
  <a-spin :spinning="confirmLoading">
    <JFormContainer :disabled="disabled">
      <template #detail>
        <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol" name="OrderForm">
          <a-row>
						<a-col :span="6">
							<a-form-item label="源订单id" v-bind="validateInfos.sourceOrderId" id="OrderForm-sourceOrderId" name="sourceOrderId">
								<a-input v-model:value="formData.sourceOrderId" placeholder="请输入源订单id"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="6">
							<a-form-item label="订单id" v-bind="validateInfos.orderNo" id="OrderForm-orderNo" name="orderNo">
								<a-input v-model:value="formData.orderNo" placeholder="请输入订单id"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="6">
							<a-form-item label="订单金额" v-bind="validateInfos.orderAmount" id="OrderForm-orderAmount" name="orderAmount">
								<a-input-number v-model:value="formData.orderAmount" placeholder="请输入订单金额" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="6">
							<a-form-item label="分拥金额" v-bind="validateInfos.reward" id="OrderForm-reward" name="reward">
								<a-input v-model:value="formData.reward" placeholder="请输入分拥金额"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="6">
							<a-form-item label="分佣占比" v-bind="validateInfos.rewardPct" id="OrderForm-rewardPct" name="rewardPct">
								<a-input-number v-model:value="formData.rewardPct" placeholder="请输入分佣占比" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="6">
							<a-form-item label="消费日期" v-bind="validateInfos.orderDate" id="OrderForm-orderDate" name="orderDate">
								<a-date-picker placeholder="请选择消费日期"  v-model:value="formData.orderDate" value-format="YYYY-MM-DD"  style="width: 100%"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="6">
							<a-form-item label="原订单信息" v-bind="validateInfos.sourceOrderInfo" id="OrderForm-sourceOrderInfo" name="sourceOrderInfo">
								<a-input v-model:value="formData.sourceOrderInfo" placeholder="请输入原订单信息"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="6">
							<a-form-item label="一级团队id" v-bind="validateInfos.teamFirstId" id="OrderForm-teamFirstId" name="teamFirstId">
								<j-search-select v-model:value="formData.teamFirstId" dict="fx_member,nickname,id"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="6">
							<a-form-item label="一级团队等级" v-bind="validateInfos.teamFirstLevel" id="OrderForm-teamFirstLevel" name="teamFirstLevel">
								<j-dict-select-tag v-model:value="formData.teamFirstLevel" dictCode="member_level" placeholder="请选择一级团队等级"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="6">
							<a-form-item label="一级团队分佣比例" v-bind="validateInfos.teamFirstPct" id="OrderForm-teamFirstPct" name="teamFirstPct">
								<a-input v-model:value="formData.teamFirstPct" placeholder="请输入一级团队分佣比例"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="6">
							<a-form-item label="一级团队分佣金额" v-bind="validateInfos.teamFirstFyamount" id="OrderForm-teamFirstFyamount" name="teamFirstFyamount">
								<a-input-number v-model:value="formData.teamFirstFyamount" placeholder="请输入一级团队分佣金额" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="6">
							<a-form-item label="二级团队id" v-bind="validateInfos.teamSecondId" id="OrderForm-teamSecondId" name="teamSecondId">
								<j-search-select v-model:value="formData.teamSecondId" dict="fx_member,nickname,id"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="6">
							<a-form-item label="二级团队等级" v-bind="validateInfos.teamSecondLevel" id="OrderForm-teamSecondLevel" name="teamSecondLevel">
								<j-dict-select-tag v-model:value="formData.teamSecondLevel" dictCode="member_level" placeholder="请选择二级团队等级"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="6">
							<a-form-item label="二级团队分佣比例" v-bind="validateInfos.teamSecondPct" id="OrderForm-teamSecondPct" name="teamSecondPct">
								<a-input v-model:value="formData.teamSecondPct" placeholder="请输入二级团队分佣比例"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="6">
							<a-form-item label="二级团队分佣金额" v-bind="validateInfos.teamSecondFyamount" id="OrderForm-teamSecondFyamount" name="teamSecondFyamount">
								<a-input-number v-model:value="formData.teamSecondFyamount" placeholder="请输入二级团队分佣金额" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="6">
							<a-form-item label="是否作废 1.是 0.否" v-bind="validateInfos.isDelete" id="OrderForm-isDelete" name="isDelete">
								<j-dict-select-tag v-model:value="formData.isDelete" dictCode="yn" placeholder="请选择是否作废 1.是 0.否"  allow-clear />
							</a-form-item>
						</a-col>
          </a-row>
        </a-form>
      </template>
    </JFormContainer>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import JSearchSelect from '/@/components/Form/src/jeecg/components/JSearchSelect.vue';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../Order.api';
  import { Form } from 'ant-design-vue';
  import JFormContainer from '/@/components/Form/src/container/JFormContainer.vue';
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: () => ({})},
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    sourceOrderId: '',   
    orderNo: '',   
    orderAmount: undefined,
    reward: '',   
    rewardPct: undefined,
    orderDate: '',   
    sourceOrderInfo: '',   
    teamFirstId: '',   
    teamFirstLevel: undefined,
    teamFirstPct: '',   
    teamFirstFyamount: undefined,
    teamSecondId: '',   
    teamSecondLevel: undefined,
    teamSecondPct: '',   
    teamSecondFyamount: undefined,
    isDelete: 0,
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = reactive({
  });
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: false });

  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });

  
  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      const tmpData = {};
      Object.keys(formData).forEach((key) => {
        if(record.hasOwnProperty(key)){
          tmpData[key] = record[key]
        }
      })
      //赋值
      Object.assign(formData, tmpData);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    try {
      // 触发表单验证
      await validate();
    } catch ({ errorFields }) {
      if (errorFields) {
        const firstField = errorFields[0];
        if (firstField) {
          formRef.value.scrollToField(firstField.name, { behavior: 'smooth', block: 'center' });
        }
      }
      return Promise.reject(errorFields);
    }
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }


  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    padding: 14px;
  }
</style>
